import moment from 'moment';

const timeFormat = 'HH:mm';

// result is 07:00 - 10:00 with 15 min interval
function amInterval() {
    let startTime = moment('07:00', timeFormat);
    startTime = startTime.subtract(15, 'minutes');
    return new Array(12).fill(0).map(() => {
        startTime = startTime.add(15, 'minutes');
        return startTime.format(timeFormat);
    });
}

// result is 10:00 - 16:00 with 30 min interval
function restTimeInterval() {
    let startTime = moment('10:00', timeFormat);
    startTime = startTime.subtract(30, 'minutes');
    return new Array(12).fill(0).map(() => {
        startTime = startTime.add(30, 'minutes');
        return startTime.format(timeFormat);
    });
}

// result is 16:00 - 19:00 with 15 min interval
function pmInterval() {
    let startTime = moment('16:00', timeFormat);
    startTime = startTime.subtract(15, 'minutes');
    return new Array(12).fill(0).map(() => {
        startTime = startTime.add(15, 'minutes');
        return startTime.format(timeFormat);
    });
}

export function addMinute(time: string, mins: number): string {
    const timeObject = moment(time, 'HH:mm');
    return timeObject.add(mins, 'minute').format('HH:mm');
}

export function timeInterval() {
    return amInterval().concat(restTimeInterval()).concat(pmInterval());
}

export function convertTimeSlotToIndex(time: string) {
    return timeInterval().findIndex((t) => moment(t, 'HH:mm').isSame(moment(time, 'HH:mm:ss')));
}
/*
export function convertIndexToTimeSlot(index: number) {
    return timeInterval()[index];
} */
